package cn.iocoder.yudao.module.system.service.problem;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.problem.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.problem.ProblemDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.problem.ProblemMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 问题列 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ProblemServiceImpl implements ProblemService {

    @Resource
    private ProblemMapper problemMapper;

    @Override
    public Long createProblem(ProblemSaveReqVO createReqVO) {
        // 插入
        ProblemDO problem = BeanUtils.toBean(createReqVO, ProblemDO.class);
        problemMapper.insert(problem);
        // 返回
        return problem.getId();
    }

    @Override
    public void updateProblem(ProblemSaveReqVO updateReqVO) {
        // 校验存在
        validateProblemExists(updateReqVO.getId());
        // 更新
        ProblemDO updateObj = BeanUtils.toBean(updateReqVO, ProblemDO.class);
        problemMapper.updateById(updateObj);
    }

    @Override
    public void deleteProblem(Long id) {
        // 校验存在
        validateProblemExists(id);
        // 删除
        problemMapper.deleteById(id);
    }

    private void validateProblemExists(Long id) {
        if (problemMapper.selectById(id) == null) {
            throw exception(PROBLEM_NOT_EXISTS);
        }
    }

    @Override
    public ProblemDO getProblem(Long id) {
        return problemMapper.selectById(id);
    }

    @Override
    public PageResult<ProblemDO> getProblemPage(ProblemPageReqVO pageReqVO) {
        return problemMapper.selectPage(pageReqVO);
    }

}